<template>
  <div class="app-container">
    <el-form
      ref="queryParams"
      :model="queryParams"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="起止时间">
        <el-date-picker
          v-model="date"
          type="daterange"
          align="left"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审题状态">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审题状态"
          clearable
        >
          <el-option
            v-for="item in option"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getRecord"
          >搜索</el-button
        >
      </el-form-item>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column prop="update_time" align="center" label="更新时间">
        </el-table-column>
        <el-table-column prop="item_id" align="center" label="题目ID">
        </el-table-column>
        <el-table-column prop="content" align="center" label="题目内容">
        </el-table-column>
        <el-table-column prop="answer" align="center" label="题目答案">
        </el-table-column>
        <el-table-column prop="options" align="center" label="错误选项">
        </el-table-column>
        <el-table-column prop="status" align="center" label="审核状态">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="OnSubmit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :total="total"
      >
      </el-pagination>
    </el-form>
  </div>
</template>

<script>
import { getRecord } from "@/api/table";
import { getToken } from "@/utils/auth";
export default {
  data() {
    return {
      // 是否启用遮罩层
      loading: false,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 总条数
      total: 0,
      // 审题状态
      option: [
        { label: "已提交", value: "已提交" },
        { label: "已作废", value: "已作废" },
        { label: "暂时跳过", value: "暂时跳过" },
      ],
      queryParams: {
        token: getToken(),
        tel: localStorage.getItem("tel"),
        start_page_count: 1,
        pageSize: 10,
        start_time: null,
        end_time: null,
        status: null,
      },
      date: null,
      tableData: [],
    };
  },
  created() {
    this.getRecord();
  },
  methods: {
    getRecord() {
      this.loading = true;
      if (this.date) {
        this.queryParams.start_time = this.date[0];
        this.queryParams.end_time = this.date[1];
      } else {
        this.queryParams.start_time = null;
        this.queryParams.end_time = null;
      }
      let obj = this.queryParams;
      getRecord(obj).then((response) => {
        // console.log(response);
        this.tableData = response.data;
        this.total = response.topic_count;
        this.loading = false;
      });
    },
    // 分页的方法
    handleCurrentChange(val) {
      this.queryParams.start_page_count = val;
      this.getRecord();
    },
    // 点击编辑
    OnSubmit(row) {
      console.log(row);
      this.$router.push({ path: "table", query: { item_id: row.item_id } });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  text-align: center;
  margin-top: 20px;
}
</style>